/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.fi.muni.pa165.hotelbookingmanager.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Andrej Galád
 */
@Stateless
public class ClientDAOImpl implements ClientDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void create(Client client) {
        if (client != null && client.getId() != null)
            throw new IllegalArgumentException("Cannot create entity with set id.");
        em.persist(client);
    }

    @Override
    public Client get(Long id) {
        return em.find(Client.class, id);
    }

    @Override
    public void update(Client client) {
        em.merge(client);
    }

    @Override
    public void delete(Client client) {
        em.remove(em.merge(client));
    }

    @Override
    public List<Client> findAll() {
        return (List<Client>)em.createQuery("SELECT c FROM Client c").getResultList();
    } 
}
